package cn.ryh.environmentvariable.controller;

import cn.ryh.environmentvariable.entity.ApplicationConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName AnnotationEnvironment
 * @Description 读取环境变量和配置文件controller
 * @Author RaoYH
 */
@RestController
@RequestMapping(value = "/readApplicationConfig")
public class ReadApplicationController {
    @Value("${spring.datasource.url}")
    private String url;

    @Resource
    private ApplicationConfig applicationConfig;

    @Resource
    private Environment env;

    /**
     * 只能通过@Resource private ApplicationConfig applicationConfig;注入
     * 这样才能获取配置文件的值，通过new 是不行的
     * @Resource 和 new 的区别
     * @Resource 相当于setter。至于何时new这个dao层对象的，是你在该对象上做了@Component注解时，
     * 这时spring就会为你实例化这个对象。然后当它看到@Resource是它就会去它的上下文中找到跟这个变量类型的相同的实例进行注入了。
     * @return
     */
    @RequestMapping(value = "/readApplicationConfig")
    public ApplicationConfig readApplicationConfig(){
        return applicationConfig;
    }

    @RequestMapping(value = "/getMyUrl")
    public String getMyUrl(){

        return url;
    }

    @RequestMapping(value = "/getServiceName")
    public String getServiceName(){
        System.out.println(env.getProperty("JAVA_HOME"));
        String property = env.getProperty("spring.datasource.driver-class-name");
        System.out.println(property);
        return property;
    }
}
